<?php
require '../../common/initPage.php';
require '../../common/dbcon.php';
require '../../common/func.php';
require '../../common/showTips.php';
require '../../common/checkForm.php';

session_start();
//拦截未登录
filterLogout();

unset($_SESSION['sendMessShowTips']);

if(!empty($_GET['dealwitharray'])) {
    if($_GET['action'] == 'reply') {
        $replyInfo = queryInfoByID($mysqlCon, queryMessByMId, $_GET['dealwitharray']);
    }else if ($_GET['action'] == 'forward'){
        $forwardInfo = queryInfoByID($mysqlCon, queryMessByMId, $_GET['dealwitharray']);
    }
}

$LCUI=queryLastConnectUserInfo($mysqlCon,queryLCU,$_SESSION['loginId']);

if($_POST['action']=='发 送'){
    if(
        isNull(array($_POST['gettoid'],$_POST['messtitle'],$_POST['messcontent'])) &&
        checkUserID($_POST['gettoid']) &&
        $_POST['gettoid']!=$_SESSION['loginId']
    ){
        $gettoidInfo=queryInfoByID($mysqlCon,queryisNull,$_POST['gettoid']);
        if(!empty($gettoidInfo)){
            $sendfromid=handledata($_POST['sendfromid']);
            $gettoid=handledata($_POST['gettoid']);
            $messtitle=handledata($_POST['messtitle']);
            $messcontent=handledata($_POST['messcontent']);
            if(empty($_POST['mid'])){
                $sendState=inNewMessage($mysqlCon,inNewMessage,$messtitle,$messcontent,$sendfromid,$gettoid,true);
            }else{
                $sendState=upSaveToSend($mysqlCon,updataNewMessage,$messtitle,$messcontent,$gettoid,$_POST['mid']);
            }
            if($sendState){
                $_SESSION['sendMessShowTips'][]='成功给用户 '.$gettoidInfo[1].' 发送一条消息';
                $_SESSION['sendMesstipsType']='success';
                unset($_POST['gettoid']);
            }else{
                $_SESSION['sendMessShowTips'][]='发送失败';
                $_SESSION['sendMesstipsType']='danger';
            }
        }else{
            $_SESSION['sendMessShowTips'][]='该接收人学号/工号不存在,请仔细核对';
            $_SESSION['sendMesstipsType']='warning';
        }
    }else{
        if (!$_POST['gettoid']) $_SESSION['sendMessShowTips'][]='接收人学号/工号不能为空';
        if ($_POST['gettoid']==$_SESSION['loginId']) $_SESSION['sendMessShowTips'][]='不能给自己发送消息';
        if (!$_POST['messtitle']) $_SESSION['sendMessShowTips'][]='消息主题不能为空';
        if (!$_POST['messcontent']) $_SESSION['sendMessShowTips'][]='消息主题不能为空';
        if (!checkUserID($_POST['gettoid'])) $_SESSION['sendMessShowTips'][]='学号/工号应由8位纯数字组成';
        $_SESSION['sendMesstipsType']='warning';
    }
}

if($_POST['action']=='存为草稿'){
    if(!empty($_POST['messtitle'])||!empty($_POST['messcontent'])||!empty($_POST['gettoid'])){
        $sendfromid=handledata($_POST['sendfromid']);
        $gettoid=handledata($_POST['gettoid']);
        $messtitle=handledata($_POST['messtitle']);
        $messcontent=handledata($_POST['messcontent']);
        if(empty($_POST['mid'])){
            $sendState=inNewMessage($mysqlCon,inNewMessage,$messtitle,$messcontent,$sendfromid,$gettoid,false);
        }else{
            $sendState=upSaveMess($mysqlCon,updataSaveMessage,$messtitle,$messcontent,$gettoid,$_POST['mid']);
        }
        if($sendState){
            $_SESSION['sendMessShowTips'][]='已成功保存到草稿';
            $_SESSION['sendMesstipsType']='success';
            unset($_POST['gettoid']);
        }else{
            $_SESSION['sendMessShowTips'][]='保存失败';
            $_SESSION['sendMesstipsType']='danger';
        }
    }else{
        $_SESSION['sendMessShowTips'][]='不能将空消息保存到草稿';
        $_SESSION['sendMesstipsType']='warning';
    }
}

$routerUrl = '../../views/public/sendmess.html';
if($_SESSION['loginIden']=='管理员'){
    require '../../views/admin/template.html';
}else{
    require '../../views/user/template.html';
}